"""
Case Type   : GUC参数--连接认证
Case Name   : gs_guc set修改参数listen_addresses为*,使用套接字和非套接字连接数据库
Create At   : 2021/04/02
Owner       : opentestcase005
Description :
    1、查看listen_addresses的默认值；
    2、设置listen_addresses参数为*
    3、使用套接字和非套接字连接
    4、恢复默认值
Expect      :
    1、listen_addresses默认值为监听本机TCP/IP地址
    2、修改listen_addresses参数为* 成功
    3、使用套接字和非套接字连接成功
    4、恢复默认值成功
History     :
"""
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOGGER = Logger()
COMMONSH = CommonSH("PrimaryDbUser")


class GUC(unittest.TestCase):
    def setUp(self):
        LOGGER.info(
            '-Opengauss_Function_Guc_Connectionauthentication_Case0300start-')
        self.constant = Constant()
        self.user_node = Node("PrimaryDbUser")

    def test_listen_addresses(self):
        LOGGER.info("步骤1：查询listen_addresses值")
        sql_cmd = COMMONSH.execut_db_sql("show listen_addresses;")
        LOGGER.info(sql_cmd)
        self.default_value = sql_cmd.split("\n")[-2].strip()
        LOGGER.info("步骤2:设置listen_addresses参数为*并重启数据库")
        sql_cmd = COMMONSH.execute_gsguc("set",
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         "listen_addresses='*'",
                                         node_name=f"{self.user_node}",
                                         single=True)
        LOGGER.info(sql_cmd)
        self.assertTrue(sql_cmd)
        sql_cmd = COMMONSH.restart_db_cluster()
        LOGGER.info(sql_cmd)
        status = COMMONSH.get_db_cluster_status()
        LOGGER.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        LOGGER.info("步骤3:使用套接字和非套接字连接数据库均成功")
        result = self.user_node.sh(f'''source {macro.DB_ENV_PATH};\
            gsql \
            -d postgres \
            -p {self.user_node.db_port} \
            -h 127.0.0.1 \
            -U {self.user_node.db_user} \
            -W {self.user_node.db_password} \
            -c "show listen_addresses;"''').result()
        LOGGER.info(result)
        self.assertIn("*", result)
        result = self.user_node.sh(f'''source {macro.DB_ENV_PATH};\
            gsql \
            -d postgres \
            -p {self.user_node.db_port} \
            -U {self.user_node.db_user} \
            -W {self.user_node.db_password} \
            -c "show listen_addresses;"''').result()
        LOGGER.info(result)
        self.assertIn("*", result)

    def tearDown(self):
        LOGGER.info("步骤4:恢复默认值")
        result = COMMONSH.execute_gsguc("set",
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f"listen_addresses="
                                        f"'{self.default_value}'",
                                        node_name=f"{self.user_node}",
                                        single=True)
        LOGGER.info(result)
        result = COMMONSH.restart_db_cluster()
        LOGGER.info(result)
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Normal" in status or "Degraded" in status)
        LOGGER.info(
            '-Opengauss_Function_Guc_Connectionauthentication_Case0300finish-')
